package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Objects;

/**
 * Created by Binsabbar on 23/09/2014.
 *
 */
public class Biblioteca {

  private Book[] books;
  private ArrayList<Book> currentBooks;
  private ArrayList<Book> checkedoutBooks;

  private Movie[] movies;
  private ArrayList<Movie> currentMovies;
  private ArrayList<Movie> checkedoutMovies;

  private String filePath = null;
  private String moviesFileName = null;
  private String booksFileName = null;

  private boolean hasBooksBeenLoaded = false;
  private boolean haveMoviesBeenLoaded = false;

  public void setFilePath(String filePath){
    if (this.filePath == null)
      this.filePath = filePath;
  }

  public void setMoviesFileName(String fileName){
    if (moviesFileName == null)
        moviesFileName = fileName;
  }

  public void setBooksFileName(String fileName){
    if (booksFileName == null)
      booksFileName = fileName;
  }

  private void loadBooks(){
    BookLoader loader = new BookLoader();
    books = loader.readBooksFromFile(filePath, booksFileName);
    currentBooks = new ArrayList<Book>(Arrays.asList(books));
    checkedoutBooks = new ArrayList<Book>(1);
    hasBooksBeenLoaded = true;
  }

  private void loadMovies(){
    MovieLoader loader = new MovieLoader();
    movies = loader.readMoviesFromFile(filePath, moviesFileName);
    if(movies != null) {
      currentMovies = new ArrayList<Movie>(Arrays.asList(movies));
      checkedoutMovies = new ArrayList<Movie>(1);
      haveMoviesBeenLoaded = true;
    }
  }

  public Book getBookByName(String bookName){
    if (!hasBooksBeenLoaded) loadBooks();
    for(Book book: books){
      if (book.bookTitle.equals(bookName)) return book;
    }
    return null;
  }

  public Movie getMovieByName(String movieName){
    if (!haveMoviesBeenLoaded) loadMovies();
    for (Movie movie : movies){
      if (movie.movieName.equals(movieName)) return movie;
    }
    return null;
  }

  public Movie[] listMovies(){
    if (!haveMoviesBeenLoaded) loadMovies();
    return currentMovies.toArray(new Movie[currentMovies.size()]);
  }

  public Book[] listBooks(){
    if (!hasBooksBeenLoaded) loadBooks();
    return currentBooks.toArray(new Book[currentBooks.size()]);
  }

  private <T> boolean checkoutItem(ArrayList<T> checkoutItems, ArrayList<T> currentItems, T item){
    if (checkoutItems.contains(item) || !(currentItems.contains(item)))
      return false;

    int itemIndex = currentItems.indexOf(item);
    Object checkedoutItem = currentItems.remove(itemIndex);
    checkoutItems.add(item);
    return true;
  }

  public boolean checkoutMovie(Movie aMovie){
    return checkoutItem(checkedoutMovies, currentMovies, aMovie);
  }

  public Boolean checkoutBook(Book aBook){
    return checkoutItem(checkedoutBooks, currentBooks, aBook);
  }

  public Boolean returnBook(Book aBook){
    if (currentBooks.contains(aBook) || !(checkedoutBooks.contains(aBook)))
      return false;

    int bookIndex = checkedoutBooks.indexOf(aBook);
    Book returnedBook = checkedoutBooks.remove(bookIndex);
    currentBooks.add(returnedBook);
    return true;
  }
}

